<?php if(!defined('WDK_PATH')) exit('No direct script access allowed');
class WdkModel
{
	protected	$dbo,
				$table,
				$where,
				$group,
				$order,
				$limit;

	
	function __construct()
	{
		$this->dbo = Wdk::dbo();
		$this->table = '';
		$this->where = array();
		$this->group = array();
		$this->order = array();
		$this->limit = '';
	}
	public function setLimit($start=0, $rows=1)
	{
		$this->limit = "{$start}, {$rows}";
	}
	public function setOrder($order=array())
	{
		$this->order = (array) $order;
	}
	public function setGroup($group=array())
	{
		$this->group = (array) $group;
	}
	public function query($sql)
	{
		return $this->dbo->execute($sql);
	}
	public function insert($data=array())
	{
		$data = (array) $data;
		$sql = $this->dbo->insertSql($this->table, $data);
		$this->dbo->execute($sql);
		if($this->dbo->affectedRows() == 0)
		{
			throw new DatabaseInsertException("Insert Error : ".$this->dbo->error());
		}
		return $this->insertId();
	}
	public function update($data=array(), $where=array())
	{
		$data = (array) $data;
		$where = (array) $where;
		$sql = $this->dbo->updateSql($this->table, $data, implode(" AND ", $where));
		$this->dbo->execute($sql);
		if($this->dbo->affectedRows() == 0)
		{
// 			throw new DatabaseUpdateException("Update Error : ".$this->dbo->error());
		}
		return $this->dbo->affectedRows();
	}
	public function delete($where=array())
	{
		$where = (array) $where;
		return self::update(array($this->table.'_del_flag'=>'T'), $where);
	}
	public function pdelete($where = array())
	{
		$where = (array) $where;
		$sql = "DELETE FROM {$this->table} WHERE ".implode(" AND ", $where);
		$this->dbo->execute($sql);
		if($this->dbo->affectedRows() == 0)
		{
			throw new DatabaseDeleteException("Update Error : ".$this->dbo->error());
		}
		return $this->dbo->affectedRows();
	}
	public function getList($fields=array('*'), $where=array(), $order=array())
	{
		$fields = (array) $fields;
		$where = (array) $where;
		$order = array_merge($this->order, (array) $order);
		$this->setOrder($order);

		$sql = sprintf(	"SELECT %s FROM `{$this->table}` %s %s %s %s"
						, (count($fields) > 0 ? implode(',', $fields) : '')
						, (count($where) > 0 ? 'WHERE '.implode(' AND ', $where) : '')
						, (count($this->group) > 0 ? 'GROUP BY '.implode(', ', $this->group) : '')
						, (count($order) > 0 ? 'ORDER BY '.implode(', ', $order) : '')
						, (strlen($this->limit) > 0) ? 'LIMIT '.$this->limit : ''
					);
		$this->dbo->execute($sql);
		if($this->dbo->numRows() > 0)
		{
			return $this->dbo->rows();    		
		}
		throw new NoRecordException();
	}
	public function getListAll($field=array('*'), $where=array(), $order=array())
	{
		$tmp = $this->limit;
		$this->limit = '';
		$data = $this->getList($field, $where, $order);
		$this->limit = $tmp;
		return $data;
	}
	public function getData($fields=array('*'), $where=array())
	{
		$fields = (array) $fields;
		$where = (array) $where;
		$sql = sprintf(	"SELECT %s FROM `{$this->table}` %s LIMIT 0,1 "
						, (count($fields) > 0 ? implode(',', $fields) : '')
						, (count($where) > 0 ? 'WHERE '.implode(' AND ', $where) : '')
					);
		$this->dbo->execute($sql);
		if($this->dbo->numRows() > 0)
		{
			return $this->dbo->row();    		
		}
		throw new NoRecordException();
	}
	public function count($where=array())
	{
		$where = (array) $where;
		$sql = sprintf(	"SELECT COUNT(*) AS `cnt` FROM `{$this->table}`%s"
						, (count($where) > 0 ? ' WHERE '.implode(' AND ', $where) : '')
					);
		$this->dbo->execute($sql);
		if($this->dbo->numRows() > 0)
		{
			$row =$this->dbo->row();
			return $row['cnt'];
		}
		throw new NoRecordException();
	}
	
	
	public function log($fk_seq=0, $memo='', $data=array())
	{
		if($data && $fk_seq && $this->table)
		{
			$log = Wdk::get('Log');
			$log->log($this->table, $fk_seq, $memo, $data);
		}
	}
	/******************************************************************************************/
	final public function allQuery()		{return $this->dbo->allQuery();}
	final public function lastQuery()		{return $this->dbo->lastQuery();}
	final public function insertId()		{return $this->dbo->insertId();}
	final public function numRows()			{return $this->dbo->numRows();}
	final public function affectedRows()	{return $this->dbo->affectedRows();}
	final public function escape($str)		{return $this->dbo->escape($str);}
	/******************************************************************************************/
	final public function transStart()		{$this->dbo->StartTrans();}
	final public function transComplete()	{$this->dbo->CompleteTrans();}
	final public function transOk()			{return $this->dbo->HasFailedTrans() == false;}
	/******************************************************************************************/
}